package controllers

import (
	"fmt"
	"github.com/beego/beego/v2/core/validation"
	beego "github.com/beego/beego/v2/server/web"
	house "house/chain/house_client"
)

type HouseController struct {
	beego.Controller
}

// GetAll 获取所有数据
func (c *HouseController) GetAllHouse() {
	result, err := house.GetAllHouses()
	c.ServeJSON()
	if err != nil {
		FailResponse(c.Controller, err.Error())
		return
	}
	fmt.Println(result)
	OkResponse(c.Controller, result, "获取成功")
}

// AddHouse 添加房屋
func (c *HouseController) AddHouse() {
	owner := c.GetString("owner")                 //主人
	housePrice := c.GetString("housePrice")       //房屋价栾�housePrice")        //房屋价格
	houseArea := c.GetString("houseArea")         //房屋面积
	houseImages := c.GetString("houseImages")     //房屋图片
	houseVerified := c.GetString("houseVerified") //房屋验证

	valid := validation.Validation{}
	valid.Required(owner, "owner").Message("房产所有者不能为空")
	valid.Required(housePrice, "housePrice").Message("房屋地址不能为空")
	valid.Required(houseArea, "houseArea").Message("房屋面积不能为空")
	valid.Required(houseImages, "houseImages").Message("房屋图片不能为空")
	valid.Required(houseVerified, "houseVerified").Message("房屋验证不能为空")

	if valid.HasErrors() {
		for _, err := range valid.Errors {
			FailResponse(c.Controller, err.Error())
		}
	}

	str, err := house.CreateHouseAsset(owner, housePrice, houseArea, houseImages, houseVerified)
	if err != nil {
		FailResponse(c.Controller, err.Error())
		return
	}

	OkResponse(c.Controller, str, "")
}

// GetHouseById 更具ID获取房屋信息
func (c *HouseController) GetHouseById() {
	houseId := c.GetString("houseId")

	valid := validation.Validation{}
	valid.Required(houseId, "houseId").Message("房屋编号不能为空")
	if valid.HasErrors() {
		for _, err := range valid.Errors {
			FailResponse(c.Controller, err.Error())
		}
	}

	result, err := house.ReadHouseAssetById(houseId)
	if err != nil {
		FailResponse(c.Controller, err.Error())
		return
	}
	OkResponse(c.Controller, result, "")
}
